#!/bin/sh

####### preprocess.sh #########
# Handle preprocess job
############################
source ./sefunctions.sh

chmod a+x *.sh
chmod a+x *.pl

workflow=$1
lfndir="/biomed/user/d/dttung/panam/outputs/$workflow"
# barcode.txt da cho vao quality.tar.gz!!!!
preprocesverify=`./pnls.sh outputs $workflow | grep "preprocess"`
if [ -n "$preprocesverify" ]; then # File has been uploaded
	echo "File preprocess.tar.gz has been uploaded by other 'preprocess' job"
	exit 0;
fi

taxoverify=`./pnls.sh outputs $workflow | grep "taxogroups"`
if [ -n "$taxoverify" ]; then # File has been uploaded
	echo "File taxogroups.txt has been uploaded by other 'preprocess' job"
	exit 0;
fi

echo -ne "Check Reference-$workflow.tar.gz ... "
if [ ! -f Reference-$workflow.tar.gz ]; then
	echo "File Reference-$workflow.tar.gz is not existed!"
	exit 1;
fi
echo "done!"
tar -xzf Reference-$workflow.tar.gz # Extract to Reference directory
echo -ne "Check preprocesstool.tar.gz ..."
# Untar/zip preprocesstool.tar.gz (this is tool files)
if [ ! -f preprocesstool.tar.gz ]; then
	echo "File preprocesstool.tar.gz is not existed"
	exit 1;
fi
echo " done!"
tar -xzf preprocesstool.tar.gz # Extract preprocesstool.tar.gz => 'bin' directory with uclust, bioperlxxx; lib directory with hmm
chmod a+x bin/*

echo -ne "Check quality.tar.gz ..." # This is the result directory got from quality step
if [ ! -f quality.tar.gz ]; then
	echo "File quality.tar.gz is not existed";
	exit 1;
fi
echo " done!";
tar -xzf quality.tar.gz # Extract quality.tar.gz => result directory and test directory which includes barcode.txt

# preprocess.pl and panam.ini are submitted in InputSandbox, no need to be checked
echo "Run preprocess"
################## DEBUG ################################
#echo "Debug 01: before run preprocess.pl" > step01.txt
#CopyToGrid $lfndir step01.txt 0 copypreprocess.log

chmod a+x preprocess.pl
./preprocess.pl panam.ini


echo -ne "Check results ... "
# Will check later!!!!
echo "done!"

echo "========================"
echo "preprocess finished successfully. Next is trimprofile ..."
echo "========================"

./trimsortsplit.sh

echo "========================"
echo "Generate taxo groups ..."
echo "========================"
./gentaxogroup.sh $workflow

# Pack the results
mv Reference/Trimmed_Profiles .
tar -czf preprocess.tar.gz results test/barcode.txt Trimmed_Profiles

# Verify taxogroups.txt again
taxoverify=`./pnls.sh outputs $workflow | grep "taxogroups"`
if [ -n "$taxoverify" ]; then # File has been uploaded
	echo "File taxogroups.txt has been uploaded by other 'preprocess' job"
	exit 0;
fi
CopyToGrid /biomed/user/d/dttung/panam/outputs/$workflow taxogroups.txt 0 copypreprocess.log


# Verify preprosess.tar.gz again
preprocesverify=`./pnls.sh outputs $workflow | grep "preprocess"`
if [ -n "$preprocesverify" ]; then # File has been uploaded
	echo "File preprocess.tar.gz has been uploaded by other 'preprocess' job"
	exit 0;
fi

# Copy the results to the grid
echo -ne "Copy the results to the grid ..."
CopyToGrid $lfndir preprocess.tar.gz 0 copypreprocess.log # No replica at the moment
echo "Done!"